// pages/enroll/enroll.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: '',
        code: '',
        password: '',
        passwords: '',
        time: '获取验证码',
        static: false,
        place_code: ''
    },
    //手机号注册
    enroll(e) {
        var that = this
        if (that.data.password != that.data.passwords) {
            wx.showToast({
                title: '两次密码不一样',
                icon: 'error',
                duration: 2000
            })
        } else {
            wx.request({
                url: 'http://fbj.wdmnm.vip/api/wxEnrollLogin',
                data: {
                    phone: that.data.phone,
                    code: that.data.code,
                    password: that.data.password,
                    place_code: that.data.place_code
                },
                dataType: 'json',
                success: function (res) {
                    //   console.log(res.data.code);
                    if (res.data.code == 200) {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'success',
                            duration: 2000,
                            success: function () {
                                setTimeout(function () {
                                    //要延时执行的代码
                                    wx.redirectTo({
                                        url: '/pages/phonelogin/phonelogin'
                                    })
                                }, 2000) //延迟时间
                            }
                        })
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'error',
                            duration: 2000
                        })
                    }
                }
            })
        }

    },
    //获取手机验证码
    codes(e) {
        var that = this
        //判断手机号是否正确
        if (!/^(13|15|17|18|19)\d{9}$/.test(that.data.phone)) {
            wx.showToast({
                title: '手机号格式错误',
                icon: 'error',
                duration: 2000
            })
            return false
        }
        //每秒钟减一 60秒限制
        let set = setInterval(function () {
            if (that.data.static != false) {
                that.setData({
                    time: that.data.time - 1
                })
                if (that.data.time <= 0) {
                    clearInterval(set)
                    that.setData({
                        static: false,
                        time: '获取验证码'
                    })
                }
            }
        }, 1000)
        wx.request({
            url: 'http://fbj.wdmnm.vip/api/wxEnroll',
            data: {
                phone: this.data.phone
            },
            dataType: 'json',
            success: function (res) {
                console.log(res.data.data);
                if (res.data.code == 200) {
                    that.setData({
                        time: 60,
                        static: true
                    })
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    that.setData({
                        time: '发送失败点击重试'
                    })
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        })
    },
    //获取手机号
    phone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    place_code(e) {
        this.setData({
            place_code: e.detail.value
        })
    },
    //获取密码
    password(e) {
        this.setData({
            password: e.detail.value
        })
    },
    //获取确认密码
    passwords(e) {
        this.setData({
            passwords: e.detail.value
        })
    },
    //获取输入的手机验证码
    code(e) {
        // console.log(e.detail.value);
        this.setData({
            code: e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})